package com.kkb.cubemall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kkb.cubemall.common.utils.R;
import com.kkb.cubemall.product.entity.CategoryBrandEntity;
import com.kkb.cubemall.product.service.BrandService;
import com.kkb.cubemall.product.service.CategoryBrandService;
import com.kkb.cubemall.product.service.CategoryService;
import com.kkb.cubemall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分类品牌关系表
 *
 * @author LiJun
 * @email 978988415@qq.com
 * @date 2021-05-18 19:42:38
 */
@RestController
@RequestMapping("product/categorybrand")
public class CategoryBrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @RequestMapping("/brands/list")
    public R brandsList(@RequestParam Integer categoryId) {
        List<BrandVo> list = categoryBrandService.getBrandsList(categoryId);
        return R.ok().put("data", list);
    }

    /**
     * 关联分类
     */
    @RequestMapping("/category/list")
    //@RequiresPermissions("product:categorybrand:list")
    public R list(@RequestParam Integer brandId) {
//        PageUtils page = categoryBrandService.queryPage(params);
        List<CategoryBrandEntity> list = categoryBrandService.list
                (new QueryWrapper<CategoryBrandEntity>().eq("brand_id", brandId));

        for (CategoryBrandEntity entity : list) {
            entity.setCategoryName(categoryService.getById(entity.getCategoryId()).getName());
            entity.setBrandName(brandService.getById(entity.getBrandId()).getName());
        }

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrand:info")
    public R info(@PathVariable("id") Integer id) {
        CategoryBrandEntity categoryBrand = categoryBrandService.getById(id);

        return R.ok().put("categoryBrand", categoryBrand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrand:save")
    public R save(@RequestBody CategoryBrandEntity categoryBrand) {
        categoryBrandService.save(categoryBrand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrand:update")
    public R update(@RequestBody CategoryBrandEntity categoryBrand) {
        categoryBrandService.updateById(categoryBrand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrand:delete")
    public R delete(@RequestBody CategoryBrandEntity categoryBrandEntity) {
//        categoryBrandService.removeByIds(Arrays.asList(ids));
        categoryBrandService.remove(
                new QueryWrapper<CategoryBrandEntity>().eq("category_id", categoryBrandEntity.getCategoryId())
                        .eq("brand_id", categoryBrandEntity.getBrandId())
        );

        return R.ok();
    }

}
